package cn.bonoon.kernel.menus;

import java.util.Properties;

import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;

import cn.bonoon.kernel.util.StringHelper;

/**
 * 插件的版本信息，即如果定义了某些插件，如：源数据管理插件、图表插件等，
 * 可以通过"cn/bonoon/plugins.properties"文件来指定该插件的一些版本信息
 * 
 * @author jackson
 *
 */
public class VersionDefinition {
	
	private final static String UNKNOW_PLUGIN = "/res/images/unknow-plugin.png";
	
	private final String version;
	private final String name;
	private final String image;
	private final String company;
	private final String time;
	private final String team;
	private final String des;
	private final String phone;

	public VersionDefinition(ModuleManager moduleManager, ApplicationContext applicationContext){
		ConfigurableListableBeanFactory clbFactory = ((ConfigurableApplicationContext)applicationContext).getBeanFactory();

		name =    moduleManager.getName();
		version = resolveEmbeddedValue(clbFactory, "current.version");
		company = resolveEmbeddedValue(clbFactory, "about.company");
		phone =   resolveEmbeddedValue(clbFactory, "about.phone");
		time =    resolveEmbeddedValue(clbFactory, "about.time");
		team =    resolveEmbeddedValue(clbFactory, "about.team");
		des =     resolveEmbeddedValue(clbFactory, "about.des");
		
		image = resolveImage(moduleManager.getResourceServer(), resolveEmbeddedValue(clbFactory, "about.image"));

	}
	
	private String resolveEmbeddedValue(ConfigurableListableBeanFactory clbFactory, String value){
		return clbFactory.resolveEmbeddedValue("${" + value + "}");
	}
	
	private String resolveImage(ServerResourceResolver resourceServer, String image){
		return resourceServer.resource(image, UNKNOW_PLUGIN);
	}
	
	VersionDefinition(ServerResourceResolver resourceServer, Properties properties){
		
		name    = properties.getProperty("about.name");
		version = properties.getProperty("about.version");
		company = properties.getProperty("about.company");
		phone   = properties.getProperty("about.phone");
		time    = properties.getProperty("about.time");
		team    = properties.getProperty("about.team");
		des     = properties.getProperty("about.des");
		
		image = resolveImage(resourceServer, properties.getProperty("about.image"));

	}
	
	public boolean parse(String id, int index, StringBuilder thuHtml, StringBuilder desHtml){
		
		if(StringHelper.isEmpty(version)) return false;
		
		thuHtml.append("<a style='padding:4px;border-style:none;border-width:0px;border-bottom-width:0px;' ");
		thuHtml.append("title='").append(name).append("' ");
		thuHtml.append("href='javascript:void(null);' ");
		thuHtml.append("id='thum-").append(id).append('-').append(index).append("' ");
		thuHtml.append("><img height='32' src='").append(image).append("'/></a>");
		
		desHtml.append("<table id='des-thum-").append(id).append('-').append(index);
		desHtml.append("' style='width:100%;");
		
		if(index > 0){
			desHtml.append("display:none;");
		}
		desHtml.append("' border='0' cellpadding='0' cellspacing='0'>");
		
		desHtml.append("<tr><td rowspan='5' align='center' valign='top' style='width:80px;'>");
		desHtml.append("<img src='").append(image).append("' height='64'/>");
		desHtml.append("</td><th align='right' style='width:60px;'>版本：</th><td>").append(version);
		desHtml.append("</td></tr><tr><th align='right'>名称：</th><td>").append(name).append("</td></tr>");
		
		if(StringHelper.isNotEmpty(company)){
			desHtml.append("<tr><th align='right'>公司：</th><td>").append(company);
			if(StringHelper.isNotEmpty(phone)){
				desHtml.append("<br/><font color='blue'>(联系电话：").append(phone).append(")</font>");
			}
			desHtml.append("</td></tr>");
		}
		
		if(StringHelper.isNotEmpty(team)){
			desHtml.append("<tr><th align='right'>团队：</th><td>").append(team);
			if(StringHelper.isNotEmpty(time)){
				desHtml.append("(时间：").append(time).append(')');
			}
			desHtml.append("</td></tr>");
		}
		
		if(StringHelper.isNotEmpty(des)){
			desHtml.append("<tr><th align='right'>说明：</th><td>").append(des).append("</td></tr>");
		}
		
		desHtml.append("</table>");
		
		return true;
	}

}
